<?php

class ConnectionsController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

    public function acceptRequestAction()
    {
        //if(!$this->request->isPost) die('Not a post');
        $sender_id = $_POST['sender_id'];
        $receiver_id = $_POST['receiver_id'];
        $result = array();

        $request = ConnectionRequestTb::findFirst(array(
            "conditions"=>"sender_id = ?0 AND receiver_id = ?1",
            "bind"=>array(0=>$sender_id,1=>$receiver_id)
            ));

        if($request == false)
        {
            
            $result['status'] = false;
            $result['messages'] = "Request is not existing";
            echo json_encode($result);
            $this->view->disable();            
            exit();
        }


        $data1 = array(
            "user_id" => $receiver_id,
            "friend_id" => $sender_id
            );

        $data2 = array(
            "friend_id" => $receiver_id,
            "user_id" => $sender_id
            );

        $this->db->begin();
        $connection = new UserConnectionsTb();
        $connection2 = new UserConnectionsTb();

        if($connection->save($data1) == false || $connection2->save($data2) == false)
        {
            $this->db->rollBack();  
            $result['status'] = false;
            $result['messages'] = "Cannot process request pls try again later";
            echo json_encode($result);
            $this->view->disable();            
            exit();
        }

        if($request->delete() == false)
        {
            $this->db->rollBack();   
            $result['status'] = false;
            $result['messages'] = "Cannot process request pls try again later";
            echo json_encode($result);
            $this->view->disable();            
            exit();
            
        }

        $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('my-channel', 'accepted-request', $data2);

        $this->db->commit();
        $result['status'] = true;
        echo json_encode($result);
        $this->view->disable();
        
        
    }
    public function requestConnectionAction()
    {
        if(!$this->request->isPost()) die('Not a post');
        $data = array(
                "sender_id"=> $this->session->get('user_id'),
                "receiver_id"=>$this->request->getPost('receiver_id')
            );
        
        $result = array();

        // validation
        // 1. CHECK IF REQUEST ALREADY SENT
        $check = ConnectionRequestTb::findFirst(array(
                'conditions' => "sender_id = ?0 AND receiver_id = ?1",
                "bind" => array(0=>$data['sender_id'],1=>$data['receiver_id'])
            ));
        if($check == true)
        {
            $result['status'] = false;
            $result['messages'] = "You have already sent request to this user";
            echo json_encode($result);
            $this->view->disable();
            exit();
        }

        $request = new ConnectionRequestTb();
        if($request->save($data) == false)
        {
            $err = '';
            foreach ($request->getMessages() as $value) {
                $err.=$value."<br>";
            }
            $result['status'] = false;
            $result['messages'] = $err;
        }
        else
        {
            $result['status'] = true;

            $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
            $pusher->trigger('my-channel', 'request-sent', $data);
        }
        echo json_encode($result);
        $this->view->disable();

    }

    public function checkAction()
    {
    	$sender_id = $this->request->getPost('sender_id');
    	$my_id = $this->session->get('user_id');
    	$result = UserConnectionsTb::checkIfConnected($my_id,$sender_id); 
    	if($result == true)
    	{
    		echo "1";
    	}
    	else
    	{
    		echo "0";
    	}
    	$this->view->disable();

    }
 	public function insertAction()
 	{

 		if(UserConnectionsTb::checkIfConnected(1,2))
 		{
 			echo "Already Friends";

 		}
 		else
 		{
 			echo UserConnectionsTb::insertConnection(1,2);
 				
 		}
 		$this->view->disable();

 	}   
}

